package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.Result;
import com.cssl.pojo.TbOrder;
import com.cssl.service.TbOrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
//@CrossOrigin
public class TbOrderController {
    @Resource
    private TbOrderService service;

    /**
     * 商品添加
     * @param tbOrder
     * @return
     */
    @PostMapping("/orderInsert")
    public TbOrder insert(@RequestBody TbOrder tbOrder){
        System.out.println("进入添加");
        if(service.saveOrUpdate(tbOrder))
            return tbOrder;
        return null;
    }

    /**
     * 商品修改
     * @param tbOrder
     * @return
     */
    @RequestMapping("/orderUpdate")
    public Result<TbOrder> updateMember(TbOrder tbOrder) {
        System.out.println("修改会员:" + tbOrder);
        //根据id改
        service.updateById(tbOrder);
        return Result.ok(service.getById(tbOrder.getId()));
    }

    /**
     * 根据商品名称模糊查询
     * @param tbOrder
     * @return
     */
    @GetMapping("/orderQuery")
    public Result query(TbOrder tbOrder,
                        @RequestParam(name="index" ) int index,
                        @RequestParam(name="size") int size){
        System.out.println("dasdasdsa------------------");
        LambdaQueryWrapper<TbOrder> lqw = Wrappers.lambdaQuery();
        lqw.like(tbOrder.getOrder_code()!=null,
                TbOrder::getOrder_code,tbOrder.getOrder_code());
        lqw.orderByDesc(TbOrder::getId);
        return Result.ok(service.page(
                new Page<TbOrder>(index,size),lqw));
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/orderDelete/api/{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean success = service.removeById(id);
        if (success) {
            return Result.ok();
        } else {
            return Result.error("删除失败");
        }
    }
}
